<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加管理员</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="权限id" prop="permissionId">
        <el-select v-model="ruleForm.permissionId" placeholder="请选择">
          <el-option
              v-for="item in permissions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="老师id" prop="teacherId">
        <el-select v-model="ruleForm.teacherId" placeholder="请选择">
          <el-option
              v-for="item in teacher"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="enable">
        <el-switch
            v-model="ruleForm.enable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#999">
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      teacher: [],
      permissions: [],
      ruleForm: {
        username:'',
        password: '',
        permissionId:'',
        teacherId:'',
        enable:1
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    permissionsList(){
      let url = "http://localhost:9088/permission";
      console.log(url);
      this.axios
          .create({'headers':{'authorization':localStorage.getItem('jwt')}})
          .get(url).then((response) =>{

        let responseBody = response.data;
        if (responseBody.state == 20000){
          let permissionList = responseBody.data;
          for(let i = 1; i < permissionList.length; i++){
            console.log("id="+permissionList[i].id+",name="+permissionList[i].name)
          }
          this.permissions = permissionList;
        }else{
          this.$message.error(responseBody.message);
        }
      })
    },
    teacherList(){
      let url = "http://localhost:9088/teacher";
      console.log(url);
      this.axios
          .create({'headers':{'authorization':localStorage.getItem('jwt')}})
          .get(url).then((response) =>{

        let responseBody = response.data;
        if (responseBody.state == 20000){
          let teachersList = responseBody.data;
          for(let i = 1; i < teachersList.length; i++){
            console.log("id="+teachersList[i].id+",name="+teachersList[i].name)
          }
          this.teacher = teachersList;
        }else{
          this.$message.error(responseBody.message);
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9088/permission/addNew';
          console.log('请求路径：' + url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('请求参数：');
          console.log(formData);
          this.axios.post(url, formData).then((response)=>{
            console.log("服务器响应结果")
            console.log(response)

            let responseBody = response.data;
            if(responseBody.state==20000){
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
            }else{
              this.$message.error(responseBody.message);
            }

          });
        } else {
          alert('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.teacherList();
    this.permissionsList();
  }
}
</script>

<style scoped>

</style>